document.ready(function() {
    setValue('startdate', get_now_date());
    setValue('menuid', getUrlParam('menuid'));
    getModuleList();
    getWorkerList();
    queryKey('ok', 'yes');
});

function getModuleList() {
    var param = {};
    param.ctrl = 'record';
    param.func = 'getModuleList';

    sendAjax(param, function(result) {
        loadOptionsItem('moduleOptions', 'moduleInfo', result.moduleList);
    });
}

function getWorkerList() {
    var param = {};
    param.ctrl = 'record';
    param.func = 'getWorkerList';

    sendAjax(param, function(result) {
        loadOptionsItem('workerOptions', 'workerInfo', result.workerList);
    });
}

function queryKey(flag, permission = 'no') {
    openLoad();
    clearSelect();
    resetPageBar(flag);

    var param = {};
    param.ctrl = 'record';
    param.func = 'getFlowlogList';
    param.moduleInfo = getAjaxValue('moduleInfo');
    param.workerInfo = getAjaxValue('workerInfo');
    param.typeInfo = getAjaxValue('typeInfo');
    param.startdate = getAjaxValue('startdate');
    param.enddate = getAjaxValue('enddate');
    param.page = getAjaxValue('page');
    param.menuid = getAjaxValue('menuid');
    param.permission = permission;

    sendAjax(param, function(result) {
        flowlog.list = result.flowlog;

        if (result.permission != null && result.permission != '') {
            flowlog.permission = result.permission;
            setDisplay('deleteMoreBtn', flowlog.permission.deleteMoreRecord == 'yes' ? '' : 'none');
        }
        
        loadPageBar(getValue('page'), result.allPages, result.rowCount);
        setHtml('listCount', result.listCount);

        setDisplay('mainBg', '');
        closeLoad();
    });
}

function deleteFlowlog(recordid) {
    setValue('tempid', recordid);
    openConfirm('确定删除吗？', tipleft, function() {
        var param = {};
        param.ctrl = 'record';
        param.func = 'deleteFlowlog';
        param.recordid = getAjaxValue('tempid');

        sendAjax(param, function(result) {
            queryKey('page');
        });
    });
}

function deleteMoreFlowlog() {
    if (isEmpty('selectid')) {
        showTip('请勾选需要删除的记录');
        return;
    }

    openConfirm('确定批量删除吗？', tipleft, function() {
        var param = {};
        param.ctrl = 'record';
        param.func = 'deleteMoreFlowlog';
        param.tempid = getAjaxValue('selectid');

        sendAjax(param, function(result) {
            queryKey('page');
        });
    });
}

function selectAll(obj) {
    var item = document.getElementsByName('isSelect');

    setValue('selectid', '');
    for (var i = 0; i < item.length; i++) {
        if (obj.checked == true) {
            item[i].checked = true;
            setValue('selectid', getValue('selectid') + item[i].value + '■');
        }
        else {
            item[i].checked = false;
        }
    }
}

function selectOne() {
    var item = document.getElementsByName('isSelect');

    setValue('selectid', '');
    for (var i = 0; i < item.length; i++) {
        if (item[i].checked) {
            setValue('selectid', getValue('selectid') + item[i].value + '■');
        }
    }
}

function clearSelect() {
    setValue('selectid', '');
    getItemByName('allSelect').checked = false;

    var item = document.getElementsByName('isSelect');
    for (var i = 0; i < item.length; i++) {
        item[i].checked = false;
    }
}